package com.laoyao.service;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Duang;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.laoyao.comm.model.Comment;
import com.laoyao.util.TextUtil;

public class CommentService {
	public static final CommentService me = Duang.duang(CommentService.class);
	private static CommentService dao = Duang.duang(CommentService.class);
	
	/**
	 * 返回视频评论列表
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public Page<Record> list(Integer pageNumber,Integer pageSize,String id){
		String sql = "select a.*,b.userName,b.photo from v_comment a,v_user b where a.userId=b.id and videoId=? order by id desc";
		return Db.paginate(pageNumber, pageSize, sql,id);	
	}
	
	/**
	 * 返回所有评论列表
	 * @param pageNumber
	 * @param pageSize
	 * @param id
	 * @return
	 */
	public Page<Record> list(Integer pageNumber,Integer pageSize,String userName,String title){
		String sql = "select a.*,b.userName,b.photo,b.id userId,c.id videoId,c.title from v_comment a,v_user b,v_video c"
				+ " where a.userId=b.id and a.videoId=c.id order by id desc";
		List list = new ArrayList();
		if(!TextUtil.isEmpty(userName)){
			sql+=" and b.userName=?";
			list.add(userName+"%");
		}
		if(!TextUtil.isEmpty(title)){
			sql+=" and c.title=?";
			list.add(title+"%");
		}
		
		return Db.paginate(pageNumber, pageSize, sql);	
	}
	
	
	public Page<Record> replyList(Integer pageNumber,Integer pageSize,String id){
		String sql = "select a.*,b.userName srcName,c.userName destName from v_comment_reply a,v_user b,v_user c"
				+ " where a.userId=b.id and a.targetId=c.id and commentId=? and a.state=1 order by id desc";
		return Db.paginate(pageNumber, pageSize, sql,id);
		
	}
	
	public boolean setState(Integer id,Integer state){
		Comment c = new Comment();
		c.setState(state);
		c.setId(id);
		return c.update();
	}

}
